{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 生成器 yield关键字\n",
    "\n",
    "def func():\n",
    "    print(\"函数调用1\")\n",
    "    yield 1\n",
    "    print(\"函数调用2\")\n",
    "    yield 2\n",
    "    print(\"函数调用3\")\n",
    "    yield 3\n",
    "    print(\"函数调用4\")\n",
    "    yield 4\n",
    "    print(\"函数调用5\")\n",
    "    yield 5\n",
    "    \n",
    "f2 = func()\n",
    "# print(f2.__next__())\n",
    "\n",
    "for f in f2:\n",
    "    print(f)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-25T08:15:12.892734Z",
     "start_time": "2024-10-25T08:15:12.889190Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def func():\n",
    "    num = yield 1\n",
    "    print(num)\n",
    "    num1 = yield num + 10\n",
    "    print(num1)\n",
    "    yield num1 + 10\n",
    "\n",
    "f3 = func()\n",
    "print(\"f3 = \", end=\"\")\n",
    "print(f3)\n",
    "print(\"f3.send(None) = \", end=\"\")\n",
    "r1 = f3.send(None)\n",
    "print(\"\\nf3.send(2) = \", end=\"\")\n",
    "r2 = f3.send(2)\n",
    "print(\"f3.send(3) = \", end=\"\")\n",
    "r3 = f3.send(3)\n",
    "print(\"r1 = \", end=\"\")\n",
    "print(r1)\n",
    "print(\"r2 = \", end=\"\")\n",
    "print(r2)\n",
    "print(\"r3 = \", end=\"\")\n",
    "print(r3)"
   ],
   "id": "f175815fe2227520",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f3 = <generator object func at 0x10808d080>\n",
      "f3.send(None) = \n",
      "f3.send(2) = 2\n",
      "f3.send(3) = 3\n",
      "r1 = 1\n",
      "r2 = 12\n",
      "r3 = 13\n"
     ]
    }
   ],
   "execution_count": 23
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
